package com.konjac.service.impl;

import com.konjac.dao.mapper.ReturnOrderMapper;
import com.konjac.dao.mapper.SaleOrderMapper;
import com.konjac.dao.mapper.ShowBookMapper;
import com.konjac.dao.mapper.ShowSupplierMapper;
import com.konjac.pojo.*;
import com.konjac.service.ReturnOrderService;
import com.konjac.until.Handle;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class ReturnOrderServiceImpl implements ReturnOrderService {
    //退货: 顾客把已买的书籍退还给书店。输出一份退货单并自动修改库存量，把本次退货的信息添加到退货表中。
    //在订单界面操作,条目后面加一个退货按钮

    public ServiceState returnOrder(Integer id) throws IOException {
        //加载mybaits核心配置文件
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //配置映射
        ReturnOrderMapper returnOrderMapper = sqlSession.getMapper(ReturnOrderMapper.class);
        ShowBookMapper showBookMapper = sqlSession.getMapper(ShowBookMapper.class);
        SaleOrderMapper saleOrderMapper = sqlSession.getMapper(SaleOrderMapper.class);

        //测试数据

        //加载实体类
        ServiceState serviceState = new ServiceState();
        ReturnOrder returnOrder = new ReturnOrder();
        SaleOrder saleOrder = new SaleOrder();
        BookInfo bookInfo = new BookInfo();


        try {
            //通过id获取订单
            saleOrder = saleOrderMapper.selectById(id);
            bookInfo = showBookMapper.selectAllByBookId(saleOrder.getBookId());
            //将sale数据转到ReturnOrder
            returnOrder.setBookId(saleOrder.getBookId());
            returnOrder.setUsername(saleOrder.getUsername());
            returnOrder.setReturnNumber(saleOrder.getSaleNumber());
            returnOrder.setReturnPrice(saleOrder.getSalePrice());
            returnOrder.setReturnTime(Handle.getDateAndTime());

            //退款订单上传数据库
            returnOrderMapper.addOrder(returnOrder);
            //库存恢复
            bookInfo.setBookQty(bookInfo.getBookQty() + returnOrder.getReturnNumber());
            showBookMapper.updateBookInfo(bookInfo);
            //删除订单表的条目
            saleOrderMapper.deleteById(id);
            //提交事务
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
            serviceState.setFlag(0);
            serviceState.setMsg("退款时出错!");
            return serviceState;
        } finally {
            sqlSession.close();
        }
        return serviceState;
    }

    public List<SaleOrder> showSale() throws IOException {
        //加载mybaits核心配置文件
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //调用dao层返回订单的信息
        SaleOrderMapper saleOrderMapper = sqlSession.getMapper(SaleOrderMapper.class);
        List<SaleOrder> saleOrderList = saleOrderMapper.selectAll();
        //System.out.println(bookInfos);
        sqlSession.close();
        return saleOrderList;
    }


}
